/*
 * Copyright (c) 2016 zhangcm. All Rights Reserved.
 */
package com.jnzc.ocr.serv.shop.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.pagination.Pagination;
import com.jnzc.ocr.common.bean.PageResult;
import com.jnzc.ocr.common.utils.*;
import com.jnzc.ocr.dependencies.serv.mybatis.MybatisServiceImpl;
import com.jnzc.ocr.dependencies.serv.utils.MybatisUtils;
import com.jnzc.ocr.serv.shop.consts.ShopDictConsts;
import com.jnzc.ocr.serv.shop.entity.ShopMember;
import com.jnzc.ocr.serv.shop.entity.ShopMember;
import com.jnzc.ocr.serv.shop.entity.ShopMemberCategory;
import com.jnzc.ocr.serv.shop.filter.ShopMemberFilter;
import com.jnzc.ocr.serv.shop.filter.ShopMemberFilter;
import com.jnzc.ocr.serv.shop.mapper.ShopMemberMapper;
import com.jnzc.ocr.serv.shop.service.ShopMemberCategoryService;
import com.jnzc.ocr.serv.shop.service.ShopMemberService;
import com.jnzc.ocr.serv.system.api.SysDictApi;
import com.jnzc.ocr.serv.system.api.SysNoApi;
import com.jnzc.ocr.serv.system.vo.DictPair;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**
 * @author zhangcm
 * @version 1.0, 2017/9/13
 */
@Service
public class ShopMemberServiceImpl extends MybatisServiceImpl<ShopMemberMapper, ShopMember, ShopMemberFilter> implements ShopMemberService {

    @Autowired
    private ShopMemberMapper shopMemberMapper;

    @Autowired
    private ShopMemberCategoryService shopMemberCategoryService;

    @Autowired
    private SysDictApi sysDictApi;

    @Autowired
    private SysNoApi sysNoApi;

    @Override
    protected void fillEntityWrapper(EntityWrapper<ShopMember> ew, ShopMemberFilter filter) {
        ShopMember entity = new ShopMember();
        entity.setId(filter.getId());
        entity.setShopId(filter.getShopId());
        ew.setEntity(entity);
        if(StringUtils.isNotEmpty(filter.getKeyword())){
            ew.like("name", filter.getKeyword()).or().like("contact_way", filter.getKeyword());
        }
    }

    @Override
    public ShopMember find(Long id, Long shopId) {
        ValidateUtils.isId(id);
        ShopMemberFilter filter = new ShopMemberFilter();
        filter.setId(id);
        filter.setShopId(shopId);
        return super.findNotNull(filter);
    }

    @Override
    public ShopMember save(ShopMember obj) {
        ValidateUtils.isId(obj.getCompanyId(), "公司ID");
        ValidateUtils.isId(obj.getShopId(), "店铺ID");
        ValidateUtils.notEmpty(obj.getName(), 20, "名称");
        ValidateUtils.notNull(obj.getAccessType(), "获取方式");
        ValidateUtils.notNull(obj.getMemberCategoryId(), "会员分类ID");
        if(null == obj.getAccessTypeName()){
            DictPair dictPair = sysDictApi.findPair(ShopDictConsts.SHOP_MEMBER_ACCESS_TYPE, obj.getAccessType());
            obj.setAccessTypeName(dictPair.getName());
        }
        if(null == obj.getMemberCategoryName()){
            ShopMemberCategory memberCategory = shopMemberCategoryService.find(obj.getMemberCategoryId(), obj.getShopId());
            obj.setMemberCategoryName(memberCategory.getName());
        }
        if(null == obj.getPoint()){
            obj.setPoint(0);
        }
        if(null == obj.getId()){
            obj.setNo(sysNoApi.getNextMemberNo(obj.getCompanyId()));
            obj.setCreateTime(DateUtils.getNow());
            shopMemberMapper.insert(obj);
        } else {
            shopMemberMapper.updateById(obj);
        }
        return obj;
    }

    @Override
    public void delete(Long id) {
        shopMemberMapper.deleteById(id);
    }

    @Override
    public void update(ShopMember member, BigDecimal changeBalanceAmount, Integer changePoint, BigDecimal changeConsumeAmount) {
        update(member, changeBalanceAmount, changePoint, changeConsumeAmount, true);
    }

    @Override
    public void update(ShopMember member, BigDecimal changeBalanceAmount, Integer changePoint, BigDecimal changeConsumeAmount, Boolean doUpdate) {
        BigDecimal balanceAmount = CalculateUtils.add(member.getBalanceAmount(), changeBalanceAmount);
        ValidateUtils.isTrue(balanceAmount.compareTo(BigDecimal.ZERO) >= 0, "余额不足");
        member.setBalanceAmount(balanceAmount);
        Integer point = CalculateUtils.add(member.getPoint(), changePoint);
        ValidateUtils.isTrue(point >= 0, "积分不足");
        member.setPoint(point);
        BigDecimal consumeAmount = CalculateUtils.add(member.getConsumeAmount(), changeConsumeAmount);
        member.setConsumeAmount(consumeAmount);
        if(doUpdate) {
            this.updateOne(member);
        }
    }
}
